package LeetCode.leetcode3;

import java.util.HashMap;

class Solution {
    public static void main(String[] args) {
        String s = " ";
        System.out.println(new Solution().lengthOfLongestSubstring(s));
    }
    public int lengthOfLongestSubstring(String s) {
        if (s == null || s.length() == 0)
            return 0;
        int slow = 0, fast = slow;
        int maxRes = 0;
        HashMap<Character, Integer> map = new HashMap<>();
        while (slow < s.length() && fast < s.length()) {
            char r = s.charAt(fast);
            if (map.containsKey(r)) {
                maxRes = Math.max(maxRes, fast - slow);
                slow = map.get(r) + 1;
                fast = slow;
                map.clear();
            }else {
                map.put(r, fast);
                fast++;
            }
        }
        if (!map.isEmpty()){
            maxRes = Math.max(maxRes, fast - slow);
        }
        return maxRes;
    }
}